﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using PreeRegisterModel;
using PreeRegisterBLL;
using System.Collections.Specialized;
using PreeRegisterBLL.Security;
using System.Runtime.InteropServices;
namespace PreeRegister_WebAPI.Controllers
{
    public class SieafController : ApiController
    {

        [HttpPost]
        /// <summary>
        /// 初始化接口
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel init(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                SieafInitResponseModel SieafInitResponseModel = new SieafInitResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "sieaf|init", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sieaf|init|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                SieafInitRequestModel sieafInitRequestModel = csSerialize.CheckSieafInitRequestModel(body_json);

                //
                int initResult = SieafService.init(sieafInitRequestModel.addr, sieafInitRequestModel.port, sieafInitRequestModel.servletEntry);
                SieafInitResponseModel.code = "1";
                SieafInitResponseModel.msg = "初始化成功";
                InitResult InitResult = new InitResult();
                InitResult.initres = initResult;
                SieafInitResponseModel.result = InitResult;
                // SieafInitResponseModel = CheckReportBLL.GetCheckReportList(SieafInitResponseModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sieaf|init|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                SieafInitResponseModel SieafInitResponseModel = new SieafInitResponseModel();
                SieafInitResponseModel.code = "-1000";
                SieafInitResponseModel.msg = "初始化失败，失败原因：" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sieaf|init|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                CommonBLL.WriteLog("sieaf|init|Return|\r\n" + err.ToString() + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }

        [HttpPost]
        /// <summary>
        /// 清理缓存
        /// </summary>
        /// <param name="any"></param>
        /// <returns></returns>
        public EncodeModel begin(NameValueCollection any)
        {
            try
            {
                //获取JSON加密参数
                string head_json = CommonBLL.RequestGetParam("head").ToString();
                string body_json = CommonBLL.RequestGetParam("body").ToString();

                //拼装jsonData类
                EncodeModel encodeModel = new EncodeModel();
                encodeModel.head = head_json;
                encodeModel.body = body_json;

                //转化jsonData串
                string jsonData = csSerialize.ResponseInfo(encodeModel);

                SieafInitResponseModel SieafInitResponseModel = new SieafInitResponseModel();

                //解密头和内容信息
                string ErrMsg = "";
                string ReturnBodyJson = "";
                if (!ResultDecode.DecodeResult(jsonData, "sieaf|begin", ref head_json, ref body_json, ref ErrMsg))
                {
                    //序列化返回内容类
                    ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                    //加密返回内容Json
                    return ResultDecode.EncodeResult(ReturnBodyJson);
                }

                //入口参数日志
                CommonBLL.WriteLog("sieaf|begin|Post|\r\n" + body_json + "\r\n\r\n");

                //反序列化请求字符串
                SieafInitRequestModel sieafInitRequestModel = csSerialize.CheckSieafInitRequestModel(body_json);

                //
                int initResult = SieafService.init(sieafInitRequestModel.addr, sieafInitRequestModel.port, sieafInitRequestModel.servletEntry);
                SieafInitResponseModel.code = "1";
                SieafInitResponseModel.msg = "初始化成功";
                InitResult InitResult = new InitResult();
                InitResult.initres = initResult;
                SieafInitResponseModel.result = InitResult;
                // SieafInitResponseModel = CheckReportBLL.GetCheckReportList(SieafInitResponseModel);

                //序列化返回内容类
                ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sieaf|begin|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
            catch (Exception err)
            {
                SieafInitResponseModel SieafInitResponseModel = new SieafInitResponseModel();
                SieafInitResponseModel.code = "-1000";
                SieafInitResponseModel.msg = "初始化失败，失败原因：" + err.Message;

                //序列化返回内容类
                string ReturnBodyJson = csSerialize.CheckSieafInitResponseModel(SieafInitResponseModel);

                //返回参数日志
                CommonBLL.WriteLog("sieaf|begin|Return|\r\n" + ReturnBodyJson + "\r\n\r\n");

                CommonBLL.WriteLog("sieaf|begin|Return|\r\n" + err.ToString() + "\r\n\r\n");

                //加密返回内容Json
                return ResultDecode.EncodeResult(ReturnBodyJson);
            }
        }



        protected static class SieafService
        {
            [DllImport("Sieaf.dll", EntryPoint = "init")]
            public static extern int init(string addr, int port, string servletEntry);

            [DllImport("Sieaf.dll", EntryPoint = "init")]
            public static extern int begin(int whandler);
        }
    }
}